<?php
class User_model extends CI_Model {

	/**
	 * @author Pwint Phyu Shwe
	 * Profile Model for tbl_user and tbl_userdetails tables
	 * CRUD of User Profile Realated.
	 * This model include pagination and related functions.
	 * Refer detail in each function's comment section below.
	 */
	// table name
	private $tbl_user = 'masteruser';
	private $tbl_userdetails = 'userdetails';

	function __construct()
	{
		// Call the Model constructor
		parent::__construct();
	}
	// get number of users in database
	function count_all(){
		return $this->db->count_all($this->tbl_user);
	}
	// get persons with paging
	function get_paged_list($limit = 10, $offset = 0){
		$this->db->order_by('userID','asc');
		return $this->db->get($this->tbl_user, $limit, $offset);
	}
	// get person by userID
	function get_by_id($userID){
		$this->db->where('userID', $userID);
		$this->db->limit(1);
		$rows = $this->db->get($this->tbl_user);
		$data = array();
		 foreach($rows->result_array() as $row) {
                $data['userID'] = $userID;
                $data['email'] = $row['emailAddress'];
                $data['displayName'] = $row['displayName'];
                $data['name'] = $row['firstName'];
                $data['phone'] = $row['phone'];
				$data['address'] = $row['address'];
				$data['image_url'] = $row['image_url'];
				$data['oldPsw'] = $row['password'];
				//timespan($row['datetime'],$time
            }
		$rows->free_result();
		return $data;
	}
	// add new person, generate activation code and send email
	function save($user){
		//$this->load->helper('string');
		//$user['activationCode'] = random_string('alnum', 7);
		$this->db->insert($this->tbl_user, $user);
		//$this->load->helper('aws_email');
		//send_email_via_ses($user['emailAddress'], 'registration@gworldtraveler.com', 'gWorldtraveler.com: Account Activation', 'Please click the following link to activate <br />' . base_url() . 'index.php/email/accountactivation/email/' . $user['emailAddress'] . '/code/' . $user['activationCode']);

		return $this->db->insert_id();
	}
	// update person by userID
	function update($cond, $person){
		if ($cond != NULL){
			foreach ($cond as  $param => $item)
				$this->db->where($param , $item);
		}
		$this->db->update($this->tbl_user, $person);
	}
	// delete person by userID
	function delete($userID){
		$this->db->where('userID', $userID);
		$this->db->delete($this->tbl_user);
	}
	/**
	* check EMAIL EXISTS (true or false)
	**/
	function email_exists($email){
		$this->db->where('emailAddress', $email);
		$query = $this->db->get($this->tbl_user);
		if($query->num_rows() > 0) 
			return true;
		else
			return false;
	}
	/**
	 * Login
	 * @param string $username
	 * @param string $password
	 * @return boolean
	 */
	function login($username, $password)
	{
		$this -> db -> select('userID, emailAddress, password, firstName, lastName, activationCode');
		$this -> db -> from('masteruser');
		$this -> db -> where('emailAddress = ' . "'" . $username . "'");
		$this -> db -> where('password = ' . "'" . MD5($password) . "'");
		$this -> db -> limit(1);

		$query = $this -> db -> get();

		if($query -> num_rows() == 1)
		{
			return $query->result();
		}
		else
		{
			return false;
		}
	}
	/**
	 * activate acount
	 * @param   	emailAddress
	 * @return      result  0 for Invalid Activationcode, 1 for Activated successfully,
	 *  2 for Invalid Email address, 3 for Account already activated, if error return false
	 */
	function activate_user($actiuser = NULL) {
		if ($actiuser == NULL)
			return false;

		$query = $this->db->get_where('masteruser', array('emailAddress' => $actiuser['emailAddress']));

		if ($query->num_rows == 1) {
			foreach ($query->result() as $row) {
				if ($row->activationCode == '') {
					return 1;
				}
				$userActivationcode = $row->activationCode;
				if ($userActivationcode == $actiuser['activationCode']) {
					$this->db->where('emailAddress', $actiuser['emailAddress']);
					$CI = get_instance();
					$CI->load->helper('date');
					$this->db->update('masteruser', array('activationCode' => '', 'activationDate' => date('Y-m-d H:i:s', now())));

					foreach ($query->result_array() as $row) {

						$userdata[] = array(
								'firstName' => $row['firstName'],
								'lastName' => $row['lastName']
						);
					}
					$query->free_result();

					return $userdata;
				} else {
					return 0;
				}
			}
		} else {
			return 0;
		}
	}
}
?>